﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WpfApp1.Commons;
using WpfApp1.DataAccess;

namespace WpfApp1.Model
{
    
    public class LoginViewModel : NotifyBase
    {
        public LoginModel LoginModel { get; set; } = new LoginModel();
        public CommandBase CloseWindowCommand { get; set; }
        public CommandBase LoginCommand { get; set; }
        private string _errorMessage;

        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value;  this.DoNotify(); }
        }
        private Visibility _showProgress = Visibility.Collapsed;

        public Visibility ShowProgress
        {
            get { return _showProgress; }
            set
            {
                _showProgress = value; this.DoNotify();
                LoginCommand.RaiseCanExecuteChanged();
            }
        }

        public LoginViewModel()//ctor加两下tab快速生成无参的构造函数
        {

            this.CloseWindowCommand = new CommandBase();
            this.CloseWindowCommand.DoExecute=new Action<object>((o)=>
            {
                (o as Window).Close();
            });
            this.CloseWindowCommand.DoCanExecute = new Func<object, bool>((o) => { return true; });

            this.LoginCommand = new CommandBase();
            this.LoginCommand.DoExecute = new Action<object>(DoLogin);
            this.LoginCommand.DoCanExecute = new Func<object, bool>((o) => { return ShowProgress == Visibility.Collapsed; });
            LoginModel.UserName = "";//防止空指针异常
            LoginModel.Password = "";//防止空指针异常
            LoginModel.VerificationCode = "";//防止空指针异常
        }
        /**
         * 这里面写登录逻辑
         */
        private  void DoLogin(object o)
        {
            this.ShowProgress = Visibility.Visible;
            this.ErrorMessage = "";
            if (string.IsNullOrEmpty(LoginModel.UserName.Trim()))
            {
                this.ErrorMessage = "用户名不能为空！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.Password.Trim()))
            {
                
                this.ErrorMessage = "密码不能为空！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            if (string.IsNullOrEmpty(LoginModel.VerificationCode.Trim()))
            {
                this.ErrorMessage = "验证码不能为空！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            if ("5374" != LoginModel.VerificationCode.Trim().ToLower())
            {
                this.ErrorMessage = "验证码错误！";
                this.ShowProgress = Visibility.Collapsed;
                return;
            }
            /*
             if ("Scott".Equals(LoginModel.UserName.Trim()) && "a".Equals(LoginModel.Password.Trim()))
            {
               
                
                this.ShowProgress = Visibility.Visible;
                this.ErrorMessage = "登录成功，正在跳转界面！";
                //采用异步的方式打开主窗口
                Application.Current.Dispatcher.Invoke(new Action(async () =>
                {
                    await Task.Delay(1000);//等待两秒
                    (o as Window).DialogResult = true;//打开主窗口
                }));
                               
            }
            else
            {
                this.ShowProgress = Visibility.Collapsed;
                this.ErrorMessage = "登录失败，用户名或密码错误！";
                return;
            }
            */

            //采用异步的方式进行连接，防止访问时间过长

            Task.Run(new Action( () =>
            {
                //async
                //await Task.Delay(2000);
                try
                {
                    var user = LocalDataAccess.GetInstance().CheckUserInfo(LoginModel.UserName, LoginModel.Password);
                    if (user == null)
                    {
                        throw new Exception("登录失败！用户名或密码错误！");
                    }

                    GrobalValues.UserInfo = user;

                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        (o as Window).DialogResult = true;
                    }));
                }
                catch (Exception ex)
                {
                    this.ErrorMessage = ex.Message;
                }
                finally
                {
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        this.ShowProgress = Visibility.Collapsed;
                    }));
                }
            }));


        }
    }
}
